package com.panshi.service.impl;

import com.panshi.entity.AppCollect;
import com.panshi.mapper.CollectMapper;
import com.panshi.service.CollectService;
import com.panshi.service.MatterService;
import com.panshi.tools.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectMapper collectMapper;

    @Autowired
    private MatterService matterService;

    @Override
    public DataResult add(AppCollect appCollect) {
        DataResult result = new DataResult();
        AppCollect collect = collectMapper.estimate(appCollect);
        if(null == collect){ // 没有收藏，执行收藏操作
            appCollect.setCollectType("a");
            collectMapper.add(appCollect);
            matterService.updateCount(appCollect.getCollectTypeId(),"collect");
            result.setCode(appCollect.getId()).setMsg("收藏成功~");
        }else{ // 有收藏，直接返回
            result.setCode(appCollect.getId()).setMsg("已收藏过该问题~");
        }
        return result;
    }

    @Override
    public void delete(Integer collectId) {
        AppCollect collect =  collectMapper.queryInfo(collectId);
        collectMapper.delete(collectId);
        if(null != collect){
            matterService.updateCount(collect.getCollectTypeId(),"collectDelete");
        }
    }
}
